<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc">

  <!-- TODO: enable links again... -->
  <xsl:template match="doc:linkkkkk">
    <basic-link color="blue">
      <xsl:choose>
        <xsl:when test="normalize-space(@ref) = '.'">
          <xsl:attribute name="internal-destination">title</xsl:attribute>
        </xsl:when>
        <xsl:when test="starts-with(@ref,'#')">
          <xsl:attribute name="internal-destination">
            <xsl:value-of select="substring(@ref,2)" />
          </xsl:attribute>
        </xsl:when>
        <xsl:when test="normalize-space(@ref) != ''">
          <xsl:attribute name="external-destination">
            <xsl:variable name="objUri">
              <xsl:value-of select="substring-before(@ref,'#')" />
              <xsl:if test="not(contains(@ref,'#'))">
                <xsl:value-of select="@ref" />
              </xsl:if>
            </xsl:variable>
            <xsl:value-of select="$objUri" />
            <xsl:if
              test="not(ends-with($objUri,'.view')
                        or ends-with($objUri,'.cosmos')
                        or ends-with($objUri,'.meta')
                        or ends-with($objUri,'.content')
                        or ends-with($objUri,'.view')
                        or ends-with($objUri,'.plain')
                       )">
               <xsl:text>.view</xsl:text>
            </xsl:if>
            <xsl:variable name="entityId" select="substring-after(@ref,'#')"/>
            <xsl:if test="normalize-space($entityId) != ''">#</xsl:if>
            <xsl:value-of select="$entityId" />
          </xsl:attribute>
        </xsl:when>
        <xsl:when test="normalize-space(@url) != ''">
          <xsl:attribute name="external-destination">
            <xsl:value-of select="@url" />
          </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="internal-destination">title</xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates select="node()"/>
    </basic-link>
  </xsl:template>

  <xsl:template match="doc:link">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

</xsl:stylesheet>
